import Mock from 'mockjs'
function param2Obj(url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
}

// 创建一个数组用来接收模拟的数据
let List = []
const count = 60

for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: Mock.Random.guid(),
    image: Mock.Random.image('40x40', '#ffcc33', '#FFF', 'png', 'user'),
    name: Mock.Random.cname(),
    phone: /^1[385][1-9]\d{8}/,
    balance:Mock.Random.integer(0,10000),
    email: '@email',
    'role|1':['admin','normal'],//随机选择一个角色
    addr: Mock.mock('@county(true)'),
    'age|18-60': 1,
    birth: Mock.Random.date(),
    sex: Mock.Random.integer(0, 1),
    status: Mock.Random.integer(0, 1)
  }))
}

export default {
  getUserList: config => {
    const { name, page = 1, limit = 5 } = param2Obj(config.url)

    const mockList = List.filter(user => {
      if (name && user.name.indexOf(name) === -1) return false
      return true
    })

    const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

    return {
      code: 0,
      data: {
        total: mockList.length,
        users: pageList
      }
    }
  },
  createUser: config => {
    // const { id, image,name, phone,balance, addr, email,role,status,age, birth, sex } = param2Obj(config.url)
    const { id,name, phone,email,role,status, sex } = param2Obj(config.url)
    // 表单提交过来后所有的表单项都已经变成了字符串，但是性格和状态在定义的时候定义的0/1，所以需要用parseInt()转换成整形
    // 如果没有转换，表单选中的值并没有真正加到表单，渲染到table中时它就会按照转换函数formatSex第三种情况未定义的值来渲染
    const status_num = parseInt(status)
    const sex_num = parseInt(sex)
    console.log(List.some(item => item.email == email));

    // 要先判断数据库中是否已经有了同一用户名，邮箱是唯一的
    if (List.some(item => item.email == email)) {
      return {
        code: 1,
        data:{
          message:'用户名已存在'
        }
      }
    } else {
      List.unshift({
        id:id,
        name: name,
        phone: phone,
        email:email,
        role: role,
        status: status_num,
        sex: sex_num
      })
      return {
        code: 0,
        data: {
          message: '添加成功'
        }
      }
    }
  },
  deleteUser: config => {
    const { id } = param2Obj(config.url)
    List = List.filter(u => u.id !== id)
    return {
      code: 0,
      data: {
        message: '删除成功'
      }
    }
  },
  batchremove: config => {
    let { ids } = param2Obj(config.url)
    ids = ids.split(',')
    List = List.filter(u => !ids.includes(u.id))
    return {
      code: 0,
      data: {
        message: '批量删除成功'
      }
    }
  },
  updateUser: config => {
    // const { id, image,name, phone , balance,email,role,addr, age, birth, sex } = param2Obj(config.url)
    // 只允许修改用户权限和用户状态
    // const { id,role,status } = param2Obj(config.url)
    // const { name, phone,email,role,status, sex } = param2Obj(config.url)
    const { id,name, phone,email,role,status, sex } = param2Obj(config.url)

    const status_num = parseInt(status)
    const sex_num = parseInt(sex)
    List.some(u => {
      if (u.id === id) {
        u.role = role
        u.status = status_num
        u.sex=sex_num
        return true
      }
    })
    return {
      code: 0,
      data: {
        message: '编辑成功'
      }
    }
  }
}
